package com.portalEsporteClubeBahia.app.bo;

import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.dao.ChampionshipDAO;
import com.portalEsporteClubeBahia.app.vo.ChampionshipVO;
import com.portalEsporteClubeBahia.app.vo.ClassificationVO;

public class ChampionshipBO {
	
	private ChampionshipDAO championshipDAO;
	
	public ChampionshipBO() throws ApplicationException{
		try {
			championshipDAO = new ChampionshipDAO();
		} catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<ChampionshipVO> findAll() throws ApplicationException{
		try{
			return championshipDAO.findAll();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<ClassificationVO> findByFilter(ClassificationVO filter) throws ApplicationException{
		try{
			return championshipDAO.findByFilter(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void insert(ChampionshipVO championship) throws ApplicationException{
		try{
			
			if(championship.getName()==null || championship.getName().equals("")){
				throw new ApplicationException("MISSING NAME!");
			}
			if(championship.getYear()==null || championship.getYear()==0){
				throw new ApplicationException("MISSING YEAR!");
			}
			
			championshipDAO.insert(championship);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public void update(ChampionshipVO championship) throws ApplicationException{
		try{
			
			if(championship.getId()==null){
				throw new ApplicationException("MISSING ID!");
			}
			if(championship.getName()==null || championship.getName().equals("")){
				throw new ApplicationException("MISSING NAME!");
			}
			if(championship.getYear()==null || championship.getYear()==0){
				throw new ApplicationException("MISSING YEAR!");
			}
			
			championshipDAO.update(championship);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}

	public void delete(ChampionshipVO championship) throws ApplicationException{
		try{
			
			if(championship.getId()==null){
				throw new ApplicationException("MISSING ID!");
			}
			
			championshipDAO.delete(championship);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public ChampionshipVO findByPrimaryKey(ChampionshipVO filter) throws ApplicationException{
		try{
			
			if(filter.getId()==null){
				throw new ApplicationException("MISSING ID!");
			}
			
			return championshipDAO.findByPrimaryKey(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}
	
	public List<ChampionshipVO> findByFilter(ChampionshipVO filter) throws ApplicationException{
		try{
			return championshipDAO.findByFilter(filter);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
	}

}
